/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.Services;


import com.ControlEscolar.Beans.Subject;
import com.ControlEscolar.DAOs.DAOException;
import com.ControlEscolar.DAOs.SubjectDAO;
import com.ControlEscolar.Exceptions.AlreadyExistExp;
import com.ControlEscolar.Exceptions.DeleteExp;
import com.ControlEscolar.Exceptions.ServiceExp;
import com.ControlEscolar.Validations.ValidationsSubject;
import java.util.ArrayList;
/**
 *
 * @author Rodna Marrufo
 */
public class SubjectService{
    
    ValidationsSubject validationSubject=new ValidationsSubject();
    SubjectDAO subjectDAO=new SubjectDAO();


    public void createSubject(Subject subject) throws  AlreadyExistExp, ServiceExp{
      try{
        if(validationSubject.isExistSubject(subject.getSubjectKey()))
            throw new AlreadyExistExp("Ya existe Materia");
        else
            subjectDAO.add(subject);}
      catch(DAOException e){
            throw new ServiceExp();
        }
    }

    public void deleteSubject(String subjectKey) throws ServiceExp, DeleteExp {
       try{
        if(validationSubject.isSubjectInReticle(subjectKey))
            throw new DeleteExp("MATERIA PERTENECE A RETICULA");
        else
            subjectDAO.delete(subjectKey);}
       catch(DAOException e){
            throw new ServiceExp();
        }
    }

    public void updateSubject(Subject subject) throws ServiceExp {
      try{
       subjectDAO.update(subject);}
      catch(DAOException e){
            throw new ServiceExp();
        }
    }

    public ArrayList<Subject> getSubjectList() throws ServiceExp {
      try{
       return subjectDAO.query();}
      catch(DAOException e){
            throw new ServiceExp();
        }
    }

    public Subject getSubject(String key) throws ServiceExp {
       try{
        return subjectDAO.queryByKey(key);}
       catch(DAOException e){
            throw new ServiceExp();
        }
    }
   



}
